package com.diyunapp.xilinapp.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.CodeCountDown;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EmailCer2Activity extends StatusBarFragmentActivity {
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.sign)
    TextView sign;
    @Bind(R.id.sign_num)
    TextView signNum;
    @Bind(R.id.recharge_money_et)
    EditText rechargeMoneyEt;
    @Bind(R.id.getCode_tv)
    TextView getCodeTv;
    @Bind(R.id.sure_to_recharge)
    TextView sureToRecharge;
    @BindColor(R.color.colorDarkTopBar)
    int pressColor;
    @BindColor(R.color.colorText0)
    int unpressColor;
    @Bind(R.id.title)
    TextView title;
    private String type;
    private String num;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_cer2);
        ButterKnife.bind(this);
        headView = findViewById(R.id.emaice2_menubar_inc);
        super.MenuWidget(headView);
        type = getIntent().getExtras().getString("type");
        num = getIntent().getExtras().getString("num");
        sign.setText("您当前绑定的" + type + "为：");
        title.setText(type+"绑定");
        signNum.setText(num);
    }

    @OnClick({R.id.go_back, R.id.getCode_tv, R.id.sure_to_recharge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.getCode_tv:
                getCodeTv.setClickable(false);
                getCodeTv.setTextColor(unpressColor);
                CodeCountDown.with(EmailCer2Activity.this).into(getCodeTv);
                String str = signNum.getText().toString().trim();
                if (str.indexOf(".com") != -1) {
                    getCode("email");
                } else {
                    getCode("mobile");
                }
                break;
            case R.id.sure_to_recharge:
                if (rechargeMoneyEt.getText().toString().trim() == null) {
                    toast("请输入验证码");
                }
                sureCode();
                break;
        }
    }

    public void getCode(String type) {
        String str = signNum.getText().toString().trim();
        String member_type = null;
        if (type.equals("email") || type == "email") {
            member_type = "member_email";
        } else if (type.equals("mobile") || type == "mobile") {
            member_type = "member_mobile";
        }
        String url = ""+MainActivity.url+"act=goods&op=send_auth_code&type=" + type + "&member_id=" + MainActivity.myLogin.getMemberid() + "&" + member_type + "=" + str + "";
        Log.i("zhangxiaoxidebug", "resultinfo=" + url);

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            String state = resultObject.optString("state");
//                            toast(String.valueOf(result));
                            if (state.equals("true")) {
                                String msg = resultObject.optString("msg");
                                toast(msg);
                            } else {
                                toast("发送失败");
                                getCodeTv.setClickable(true);
                                getCodeTv.setTextColor(pressColor);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");

                    }
                });
    }

    private void sureCode() {
        String code = rechargeMoneyEt.getText().toString().trim();
        String url = ""+MainActivity.url+"act=goods&op=auth&auth_code=" + code + "&member_id=" + MainActivity.myLogin.getMemberid() + "";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int code = resultObject.optInt("code");
//                            toast(String.valueOf(result));
                            if (code == 200) {

                                toast("验证成功");
                                String str = signNum.getText().toString().trim();
                                if (str.indexOf(".com") != -1) {
                                    nextAct(EmailCerActivity.class);
                                } else {
                                    nextAct(PhoneCerActivity.class);
                                }

                            } else {
                                String data = resultObject.optString("data");
                                toast(data);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");

                    }
                });
    }

    public void nextAct(Class act){
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }
}
